import { ContentModel, SelectionModel } from "../models";

export default class Render {
  private element!: HTMLElement
  constructor(element: HTMLElement) {
    this.element = element
  }

  renderContent(contentModel: ContentModel) {
    this.element.innerHTML = `<pre>${contentModel.body}\n</pre>`
  }
  renderSelection(selectionModel: SelectionModel) {
    if(!this.element.childNodes[0]) return
    if(!this.element.childNodes[0].childNodes[0]) return
    const domSelection = window.getSelection()
    if (domSelection) {
      const range = window.document.createRange()
      range.setStart(this.element.childNodes[0].childNodes[0], selectionModel.index)
      range.setEnd(this.element.childNodes[0].childNodes[0], selectionModel.endIndex)
      domSelection.removeAllRanges()
      domSelection.addRange(range)
    }
  }
}